package threadpool;

import thread.ThreadUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

public class ThreadFactoryDemo {

    private static final ExecutorService threadPool = ThreadPoolFactory.getThreadPool();

    /**
     * 如果不关闭线程池，会一直执行
     */
    public static void main(String[] args) throws Exception {
        execute();
        submitRunnable();
        submitCallable();
        threadPool.shutdown();
    }

    private static void execute() {
        for (int i = 0; i < 10; i++) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    ThreadUtils.sleep(100);
                    System.out.println("execute：" + Thread.currentThread().getName());
                }
            });
        }
    }

    private static void submitRunnable() {
        for (int i = 0; i < 10; i++) {
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    ThreadUtils.sleep(100);
                    System.out.println("submitRunnable：" + Thread.currentThread().getName());
                }
            });
        }
    }

    private static void submitCallable() throws Exception {
        for (int i = 0; i < 10; i++) {
            Future<String> callableString = threadPool.submit(new Callable<String>() {
                @Override
                public String call() {
                    ThreadUtils.sleep(100);
                    return "submitCallable：" + Thread.currentThread().getName();
                }
            });
            System.out.println(callableString.get());
        }
    }
}
